// This File is part of the "nyaqt" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace nyaqt
{
    public partial class ChangeProviderForm : Form
    {
        public ConnectionInfo SelectedConnection;
        DataTable providerList;
        ConnectionInfoList _cl;
        int _version = 0;

        public int Version
        {
            get { return _version; }
            set { _version = value; }
        }


        public ChangeProviderForm()
        {
            InitializeComponent();
        }

        DataTable _pre20Table = null;
        private DataTable GetPre20FactoryClasses()
        {
            if (_pre20Table == null)
            {
                _pre20Table = new DataTable();
                _pre20Table.Columns.Add("Name", typeof(string));
                _pre20Table.Columns.Add("Description", typeof(string));
                _pre20Table.Columns.Add("InvariantName", typeof(string));
                foreach (Pre20ProviderFactoriesElement el in My.Settings.Pre20ProviderFactories.Pre20Providers)
                    _pre20Table.Rows.Add(el.Name, el.Description, el.InvariantName);
            }
            return _pre20Table.Copy();
        }

        private void ChangeProviderForm_Load(object sender, EventArgs e)
        {
            if (_version == 1)
            {
                providerList = GetPre20FactoryClasses();
            }
            else if (_version == 2)
                providerList = DbProviderFactories.GetFactoryClasses();

            this.AcceptButton = newConn;
            lastConn.Enabled = false;
            RecentScroll.Maximum = 10;
            RecentScroll.Minimum = 0;
            RecentScroll.Enabled = false;
            recentConnString.Text = "";
            
            foreach (DataRow provider in providerList.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = provider;
                item.Text = provider["Name"] as string;
                item.SubItems.Add(provider["Description"] as string,SystemColors.Control,SystemColors.Window,listView1.Font);
                listView1.Items.Add(item);
                if (SelectedConnection != null && SelectedConnection.ProviderInvariantName == provider["InvariantName"] as string && SelectedConnection.Version == _version)
                {
                    item.Selected = true;
                    item.EnsureVisible();
                    listView1_SelectedIndexChanged(null, null);
                }
            }
        }


        private void listView1_Resize(object sender, EventArgs e)
        {
            listView1.TileSize = new Size(listView1.ClientSize.Width-10, listView1.TileSize.Height);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;

            DataRow selectedProvider = listView1.SelectedItems[0].Tag as DataRow;
            string sel = selectedProvider["InvariantName"] as string + ";" + _version;
            if (My.Settings.RecentConnections.Contains(sel))
                _cl = My.Settings.RecentConnections[sel];
            else
                _cl = null;
            //Retrieve recent connection string
            //if empty, change default button to 'newConn' else 'lastConn'
            if (_cl == null)
            {
                this.AcceptButton = newConn;
                lastConn.Enabled = false;
                RecentScroll.Maximum = 10;
                RecentScroll.Minimum = 0;
                RecentScroll.Enabled = false;
                recentConnString.Text = "";
            }
            else
            {
                this.AcceptButton = lastConn;
                //newConn.Enabled = false;

                RecentScroll.Enabled = true;
                RecentScroll.Maximum = _cl.Connections.Count - 1;
                RecentScroll.Minimum = 0;
                RecentScroll_ValueChanged(null, null);
            }

            (this.AcceptButton as Button).Enabled = true;
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.AcceptButton.PerformClick();
        }

        private void RecentScroll_ValueChanged(object sender, EventArgs e)
        {
            recentConnString.Text = _cl.Connections[RecentScroll.Value].ConnectionString;
        }

        private void newConn_Click(object sender, EventArgs e)
        {
            DataRow selectedProvider = listView1.SelectedItems[0].Tag as DataRow;
            SelectedConnection = new ConnectionInfo();
            SelectedConnection.ProviderInvariantName = selectedProvider["InvariantName"] as string;
            SelectedConnection.Version = _version;
            SelectedConnection.ProviderName = selectedProvider["Name"] as string;
            this.DialogResult = DialogResult.OK;
        }

        private void lastConn_Click(object sender, EventArgs e)
        {
            SelectedConnection = _cl.Connections[RecentScroll.Value].Copy();
            this.DialogResult = DialogResult.OK;
        }
    }
}
































